package com.yoga.manager.admin.impl;

import com.yoga.common.appexception.AppErrorCodeEnum;
import com.yoga.common.appexception.AppException;
import com.yoga.common.bo.PrivateVipBO;
import com.yoga.dao.dao.PrivateVipDao;
import com.yoga.dao.domain.PlainVipDO;
import com.yoga.dao.domain.PrivateVipDO;
import com.yoga.manager.admin.AdminPrivateVipManager;
import com.yoga.manager.beanUtil.PlainVipBeanUtil;
import com.yoga.manager.beanUtil.PrivateVipBeanUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * Created by hdx on 2020/10/5.
 */
@Component
public class AdminPrivateVipManagerImpl implements AdminPrivateVipManager {
    Logger logger = LoggerFactory.getLogger(AdminPrivateVipManagerImpl.class);

    @Resource
    private PrivateVipDao privateVipDao;

    @Override
    public Boolean openPrivateVip(PrivateVipBO privateVipBO) throws AppException {
        if (privateVipBO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (privateVipBO.getPrivateVipId() == null) {
            throw new AppException(AppErrorCodeEnum.ID_IS_NULL);
        }
        if (privateVipBO.getClassNum() == null || privateVipBO.getClassNum() < 0) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (StringUtils.isEmpty(privateVipBO.getVipPrice())) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        PrivateVipDO old = privateVipDao.findById(privateVipBO.getPrivateVipId());
        if (old == null) {
            throw new AppException(AppErrorCodeEnum.QUERY_DATA_NULL);
        }
        PrivateVipDO update = PrivateVipBeanUtil.privateVipBOToDO(privateVipBO);
        Double d = Double.parseDouble(old.getTotalPrice()) + Double.parseDouble(privateVipBO.getVipPrice());
        update.setTotalPrice(d.toString());
        int num = privateVipDao.updateById(update);
        if (num <= 0) {
            throw new AppException(AppErrorCodeEnum.UPDATE_DATA_NULL);
        }

        return Boolean.TRUE;
    }

    @Override
    public PrivateVipBO getByUserId(Long yogaUserId) throws AppException {
        if (yogaUserId == null) {
            throw new AppException(AppErrorCodeEnum.YOGA_USER_ID_NULL);
        }
        PrivateVipDO privateVipDO = privateVipDao.findByYogaUserId(yogaUserId);

        return PrivateVipBeanUtil.privateVipDOToBO(privateVipDO);
    }

    @Override
    public Boolean updatePrivateVip(PrivateVipBO privateVipBO) throws AppException {
        if (privateVipBO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (privateVipBO.getPrivateVipId() == null) {
            throw new AppException(AppErrorCodeEnum.ID_IS_NULL);
        }

        PrivateVipDO privateVipDO = PrivateVipBeanUtil.privateVipBOToDO(privateVipBO);
        int num = privateVipDao.updateById(privateVipDO);
        if (num <= 0) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }

        return Boolean.TRUE;
    }
}
